package com.aboo.util;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * DES加解密工具箱
 * @author Yata Aboo
 *
 */
public class DESUtil {

	/**
	 * 加密
	 * @param rawStr  待加密的值
	 * @param rawKey  密钥值
	 * @return
	 * @throws InvalidKeyException
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeySpecException
	 * @throws NoSuchPaddingException
	 * @throws BadPaddingException
	 * @throws IllegalBlockSizeException
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	public static String encrypt(String rawStr, String rawKey)
			throws InvalidKeyException, NoSuchAlgorithmException,
			InvalidKeySpecException, NoSuchPaddingException,
			BadPaddingException, IllegalBlockSizeException,
			IllegalStateException, IOException {
		byte rawStrData[] = rawStr.getBytes();
		byte rawKeyData[] = rawKey.getBytes();
		byte encryptData[] = encryptString(rawStrData, rawKeyData);
		String encryptStr = (new BASE64Encoder()).encode(encryptData);
		byte c[] = encryptStr.getBytes();
		StringBuffer strBuf = new StringBuffer();
		for (int i = 0; i < c.length; i++) {
			int t = c[i];
			String str = Integer.toHexString(t);
			if (str.length() == 1)
				strBuf.append(0);
			strBuf.append(Integer.toHexString(t));
		}

		return strBuf.toString();
	}

	/**
	 * 解密
	 * @param encryptStr  待解密的值
	 * @param rawKey  密钥
	 * @return
	 * @throws InvalidKeyException
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeySpecException
	 * @throws NoSuchPaddingException
	 * @throws BadPaddingException
	 * @throws IllegalBlockSizeException
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	public static String decrypt(String encryptStr, String rawKey)
			throws InvalidKeyException, NoSuchAlgorithmException,
			InvalidKeySpecException, NoSuchPaddingException,
			BadPaddingException, IllegalBlockSizeException,
			IllegalStateException, IOException {
		char c[] = new char[encryptStr.length() / 2];
		StringBuffer sbuf = new StringBuffer();
		for (int i = 0; i < c.length; i++) {
			int a = Integer
					.parseInt(encryptStr.substring(i * 2, i * 2 + 2), 16);
			c[i] = (char) a;
			sbuf.append(c[i]);
		}

		encryptStr = sbuf.toString();
		BASE64Decoder decoder = new BASE64Decoder();
		byte encryptedData[] = decoder.decodeBuffer(encryptStr);
		byte rawKeyData[] = rawKey.getBytes();
		byte decryptData[] = getDecrypt(encryptedData, rawKeyData);
		return new String(decryptData);
	}

	private static byte[] encryptString(byte data[], byte rawKeyData[])
			throws InvalidKeyException, NoSuchAlgorithmException,
			InvalidKeySpecException, NoSuchPaddingException,
			BadPaddingException, IllegalBlockSizeException,
			IllegalStateException {
		SecureRandom sr = new SecureRandom();
		DESKeySpec dks = new DESKeySpec(rawKeyData);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		javax.crypto.SecretKey key = keyFactory.generateSecret(dks);
		Cipher cipher = Cipher.getInstance("DES");
		cipher.init(1, key, sr);
		byte encryptedData[] = cipher.doFinal(data);
		return encryptedData;
	}

	private static byte[] getDecrypt(byte encryptedData[], byte rawKeyData[])
			throws InvalidKeyException, NoSuchAlgorithmException,
			InvalidKeySpecException, NoSuchPaddingException,
			BadPaddingException, IllegalBlockSizeException,
			IllegalStateException {
		DESKeySpec dks = new DESKeySpec(rawKeyData);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		javax.crypto.SecretKey key = keyFactory.generateSecret(dks);
		Cipher cipher = Cipher.getInstance("DES");
		cipher.init(2, key);
		byte decryptedData[] = cipher.doFinal(encryptedData);
		return decryptedData;
	}

}
